package com.lucweb.dao.impl;

import com.lucweb.dao.CompanyDao;

import com.lucweb.entity.TaskCompany;
import org.hibernate.SessionFactory;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Repository("companyDao")
public class CompanyDaoImpl implements CompanyDao {


    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;


    @Override
    public List<TaskCompany> companyList(String para) {
        List<TaskCompany> companyList = (List<TaskCompany>) sessionFactory.getCurrentSession().createQuery("FROM TaskCompany where companyName ='"+para+"'").list();

        return companyList;
    }

    @Override
    public void saveOrUpdate(TaskCompany taskcompany) {
        sessionFactory.getCurrentSession().saveOrUpdate(taskcompany);


    }

    @Override
    public TaskCompany getcompanyById(int taskId) {
        return (TaskCompany) sessionFactory.getCurrentSession().get(TaskCompany.class, taskId);
    }

    @Override
    public void deletecompany(int taskId) {

        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskCompany.class, taskId)
        );
    }

    @Override
    public List<TaskCompany> showcompany() {
        List<TaskCompany> companyList = (List<TaskCompany>) sessionFactory.getCurrentSession().createQuery("FROM TaskCompany ").list();

        return companyList;
    }

    @Override
    public int getmaxId() {
        Integer number = (Integer) sessionFactory.getCurrentSession().createSQLQuery("select auto_increment from " +
                "information_schema.tables where table_schema='firesystem' and table_name='task_company';")
                .addScalar("auto_increment", StandardBasicTypes.INTEGER).uniqueResult();
        return number;
    }
}
